<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 用a标签来改变url -->
    <p>
      <a href="#hot">热映</a> | <a href="#cinema">影院</a> |
      <a href="#wait">待映</a>
    </p>

    <!-- 需要一个容器，来存放等下切换的内容 -->
    <div class="box"></div>

    <script>
      // 需要一个路由表
      const route = [
        {
          url: "#hot",
          template: "<div>这是热映页面</div>",
        },
        {
          url: "#cinema",
          template: "<div>这是影院页面</div>",
        },
        {
          url: "#wait",
          template: "<div>这是待映页面</div>",
        },
      ];

      // 监听url的变化
      window.addEventListener("hashchange", () => {
        console.log("url改变了");
        console.log(location.hash);
        route.forEach((item) => {
          if (item.url === location.hash) {
            document.querySelector(".box").innerHTML = item.template;
          }
        });
      });

      route.forEach((item) => {
        if (item.url === location.hash) {
          document.querySelector(".box").innerHTML = item.template;
        }
      });
    </script>
  </body>
</html>
